import { defineStore } from 'pinia'
import { useUserStore } from './user'
import { hasIsUserLogin } from '@/utils'
import { Router } from 'vue-router'

export const useRouterStore = defineStore('routerStore', {
  // todo: 这里没有进行类型标注
  state() {
    return {
      // 侧边栏
      asideList: [] as any,
      addRouteResultNames: [] as string[],
      routes: [] as any[],
    }
  },

  actions: {
    // 根据用户获取不同的侧边栏列表
    async getAsideList() {
      // const userStore = useUserStore()
      // let result = []
      // if (!hasIsUserLogin()) {
      //     // 用户未登陆
      //     // result = await getAsideList({ permission: 3 })
      // } else {
      //     // 获取当前登陆用户的权限
      //     const permission = userStore.userInfo?.permission
      //     // result = await getAsideList({ permission })
      // }
      // if (Array.isArray(result)) {
      //     this.asideList = result.map((item) => {
      //         return {
      //             ...item,
      //             title: item.name,
      //         }
      //     })
      // }
    },

    updateAddRouteResults(results: any[]) {
      this.addRouteResultNames = results
    },

    // 删除动态添加的路由
    deleteActiveRoute(router: Router) {
      this.addRouteResultNames.forEach((name) => {
        router.removeRoute(name)
      })
      this.routes = router.getRoutes()
    },
  },
})
